﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Activation;
using System.IO;
using System.Web.Script.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

using AKMII.DMRA.Business.Management;
using AKMII.DMRA.Common;

namespace AKMII.DMRA.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MapService : IMapService
    {
        private ZipManager zipManager;

        public MapService()
        {
            zipManager = new ZipManager();
        }

        #region IMapService Members

        public string GetZips(string userId, int month, int year, int type)
        {
            List<LeadBlock> zips = new List<LeadBlock>();
            string zipStr = string.Empty;

            try
            {
                Logger.Info("Request from " + userId);

                if (!string.IsNullOrEmpty(userId))
                {
                    zips = zipManager.GetZipsById(userId, month, year, type);
                }
                else
                {
                    Logger.Info("MD name is null or empty.");
                }

                JavaScriptSerializer serializer = new JavaScriptSerializer(new SimpleTypeResolver());
                zipStr= serializer.Serialize(zips);

                Logger.Info("Request end " + userId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get zips service", ex);
            }

            return zipStr;
        }

        public string GetZipsById(string userId, int month, int year, int type)
        {
            List<LeadBlock> zips = new List<LeadBlock>();
            StringBuilder zipStringBuilder = new StringBuilder(string.Empty);           

            try
            {
                Logger.Info("Request from " + userId);

                if (!string.IsNullOrEmpty(userId))
                {
                    zips = zipManager.GetZipsById(userId, month, year, type);
                }
                else
                {
                    Logger.Info("MD name is null or empty.");
                }

                foreach (var zip in zips)
                {
                    XmlSerializer xmlSerializer = new XmlSerializer(typeof(LeadBlock));
                    XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
                    xmlSerializerNamespaces.Add(string.Empty, string.Empty);
                    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
                    xmlWriterSettings.OmitXmlDeclaration = true;

                    using (XmlWriter writer = XmlWriter.Create(zipStringBuilder, xmlWriterSettings))
                    {
                        xmlSerializer.Serialize(writer, zip, xmlSerializerNamespaces);
                    }
                }

                Logger.Info("Request end " + userId);
            }
            catch (Exception ex)
            {
                Logger.Error("Exception occurs in get zips by id service", ex);
            }

            return zipStringBuilder.ToString();
        }

        #endregion
    }
}
